package com.jt.www.biz.agree.controller;

import com.jt.www.biz.agree.service.AgreePrivacyLogService;
import com.jt.www.domain.enums.wxb.ProtocolTypeEnum;
import com.jt.www.domain.po.AgreePrivacyLogEntity;
import com.jt.www.model.reps.GenericDataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author lq
 * @create 2020-01-07 10:40
 */
@Api(value = "隐私声明管理", tags = {"AGREE-PRIVACY-LOG-API"}, description = "隐私声明管理")
@RestController
@RequestMapping(value = "/agreePrivacyLog")
public class AgreePrivacyLogController {

    public static final Logger log = LoggerFactory.getLogger(AgreePrivacyLogController.class);

    @Autowired
    private AgreePrivacyLogService agreePrivacyLogService;


    /**
     * 保存隐私声明勾选记录信息
     *
     * @param agreePrivacyLogEntity
     * @return
     */
    @ApiOperation(value = "保存隐私声明勾选记录信息", notes = "保存隐私声明勾选记录信息", httpMethod = "POST")
    @PostMapping(value = "/save")
    public ResponseEntity saveAgreePrivacyLog(@RequestBody AgreePrivacyLogEntity agreePrivacyLogEntity) {
        if (StringUtils.isBlank(agreePrivacyLogEntity.getProtocolType())) {
            agreePrivacyLogEntity.setProtocolType(ProtocolTypeEnum.getDefaultTypeByChannelCode(agreePrivacyLogEntity.getChannelCode()));
        }

        Integer num = agreePrivacyLogService.saveAgreePrivacyLog(agreePrivacyLogEntity);
        if (num == 1) {
            return GenericDataResponse.ok();
        } else if (num == -1) {
            return GenericDataResponse.errorWithMsg("参数不能为空");
        } else if (num == 0) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.errorWithMsg("保存隐私声明勾选记录信息失败");
    }


    /**
     * 查询隐私声明勾选记录信息
     *
     * @param usercode
     * @param channelCode
     * @return
     */
    @ApiOperation(value = "查询隐私声明勾选记录信息", notes = "查询隐私声明勾选记录信息", httpMethod = "GET")
    @GetMapping(value = "/search")
    public ResponseEntity searchAgreePrivacyLog(@ApiParam(value = "用户userCode", required = true) @RequestParam("usercode") String usercode,
                                                @ApiParam(value = "登录渠道：edu-教保网；wxb-我学保；", required = true) @RequestParam("channelCode") String channelCode,
                                                //暂时为false 前端上线后为true
                                                @ApiParam(value = "协议类型", required = true) @RequestParam("protocolType") String protocolType) {

        if (StringUtils.isEmpty(usercode)) {
            log.error("【根据userCode、channelCode查询隐私声明勾选记录信息】userCode或channelCode为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        if (StringUtils.isBlank(protocolType)) {
            protocolType = ProtocolTypeEnum.getDefaultTypeByChannelCode(channelCode);
        }
        return agreePrivacyLogService.searchAgreePrivacyLog(usercode, channelCode, protocolType);
    }
}
